package com.colorful.insuranceproject.request;

import android.os.Handler;
import android.text.TextUtils;

import com.colorful.insuranceproject.bean.PersonInfo;
import com.colorful.insuranceproject.bean.PersonInfoBean;
import com.colorful.insuranceproject.bean.VerifyBean;
import com.colorful.insuranceproject.bean.VersionBean;
import com.colorful.insuranceproject.consts.URLConst;
import com.lovely3x.common.managements.user.IUser;
import com.lovely3x.common.requests.impl.HandlerRequestImpl;
import com.lovely3x.common.utils.ALog;
import com.lovely3x.common.utils.Response;
import com.lovely3x.jsonparser.model.JSONObject;
import com.lovely3x.loginandresgiter.bean.SimpleUser;

/**
 * 关于一些用户相关的请求放在这里
 * Created by lovely3x on 16/8/4.
 */
public class UserRequest extends BaseRequest {

    public UserRequest(Handler handler) {
        super(handler);
    }

    /**
     * 获取用户信息
     *
     * @param uid 需要获取用户信息的用户id,如果想要获取自己的信息可以不传
     */
    public Response getUserInfoSync(String uid, int what) {
        if (TextUtils.isEmpty(uid)) {
            return processSyncWithJSON(URLConst.GET_USER_INFO, what);
        } else {
            return processSyncWithJSON(URLConst.GET_USER_INFO, what, "otheruid", uid);
        }
    }

    /**
     * 退出登录
     */
    public void loginOut(int loginOut) {
        processWithJSON(new HandlerRequestImpl.EmptyProcessResult(), URLConst.LOGIN_OUT, loginOut);
    }

    /**
     * 更新用户信息接口
     */
    public boolean updateUserInfoSync(IUser user) {
        boolean isSucceed = false;
        try {
            Response resp = getUserInfoSync(null, -1);
            if (resp.isSuccessful) {

               JSONObject jo = (JSONObject) resp.obj;
                final PersonInfoBean udi = jo.getJSONObject(DATA_KEY).createObject(PersonInfoBean.class);

                PersonInfo pi = udi.getPersonInfo();

                if (user instanceof SimpleUser) {
                    SimpleUser simpleUser = (SimpleUser) user;

                    if (pi.getResultPhone() == null || pi.getResultPhone().equals("") || pi.getResultPhone().equals("null")) {
                        simpleUser.setUserPhone("null");//电话号码
                    } else {
                        simpleUser.setUserPhone(pi.getResultPhone());//电话号码
                    }
                    isSucceed = true;
                }
            }
        } catch (Exception e) {
            ALog.e(TAG, e);
        } finally {
            //ignore
        }
        return isSucceed;
    }

    /**
     * 获取注册的验证码
     *
     * @param phone 传递的验证码
     * @param what  handler传递值
     */
    public void getRegisterVerifyCodeUrl(String phone, int what) {
        process(new ProcessResult() {
            @Override
            public void onResult(Response res, JSONObject result) throws Exception {
                if (res.isSuccessful) {
                    ALog.e(TAG, "获取注册的验证码----------------->" + result);
                }
            }
        }, URLConst.GET_REGISTER_VERIFY_CODE_URL, what, "userPhone", phone);
    }

    /**
     * 修改登陆密码
     *
     * @param oldPwd 旧密码
     * @param newPwd 新密码
     * @param what   handler传递值
     */
    public void modifyLoginPasswordUrl(String oldPwd, String newPwd, int what) {
        process(new ProcessResult() {
            @Override
            public void onResult(Response res, JSONObject result) throws Exception {
                if (res.isSuccessful) {
                    ALog.e(TAG, "修改登陆密码------------->" + result);
                }
            }
        }, URLConst.MODIFY_LOGIN_PASWWORD_URL, what, "oldPassword", oldPwd, "newPassword", newPwd);
    }

    /**
     * 注册判断验证码
     *
     * @param registerKey 被打包后的账号+验证码
     * @param what        handler传递值
     */
    public void judgeVerifyCodeByRegister(final String registerKey, String code, int what) {
        process(new ProcessResult() {
            @Override
            public void onResult(Response res, JSONObject result) throws Exception {
                if (res.isSuccessful) {
                    ALog.e(TAG, "注册判断验证码------------>" + res);
                }
            }
        }, URLConst.JUDGE_VERIFY_CODE_BY_REGISTER_URL, what, "mobileCode", code, "userPhone", registerKey);
    }

    /**
     * 注册接口
     *
     * @param phoneNum 电话号码
     * @param what     handler传递值
     */
    public void registerAccount(String phoneNum, int type, String mobileCode, int what) {
        process(new ProcessResult() {
            @Override
            public void onResult(Response res, JSONObject result) throws Exception {
                if (res.isSuccessful) {
                    ALog.e(TAG, "注册接口------------>" + result);
                }
            }
        }, URLConst.REGISTER_NEW_USER_ULR, what, "registerKey", phoneNum, "nameType", type, "mobileCode", mobileCode);
    }

    /**
     * 忘记密码获取验证码
     *
     * @param phone 电话号码
     * @param what  handler传递值
     */
    public void getForgotPasswordVerifyCodeUrl(String phone, int what) {
        process(new ProcessResult() {
            @Override
            public void onResult(Response res, JSONObject result) throws Exception {
                if (res.isSuccessful) {
                    ALog.e(TAG, "忘记密码获取验证码--------------->" + result);
                }
            }
        }, URLConst.GET_FORGOT_PASSWORD_VERIFYCODE_URL, what, "userPhone", phone);
    }


    /**
     * 忘记密码判断验证码
     *
     * @param registerKey 被打包后的账号+验证码
     * @param what        handler传递值
     */
    public void judgeVerifyCodeByForgot(final String registerKey, String code, int what) {
        process(new ProcessResult() {
            @Override
            public void onResult(Response res, JSONObject result) throws Exception {
                if (res.isSuccessful) {
                    ALog.e(TAG, "注册判断验证码------------>" + res);
                }
            }
        }, URLConst.JUDGE_VERIFY_CODE_BY_FORGOT_URL, what, "mobileCode", code, "userPhone", registerKey);
    }

    /**
     * 忘记密码  设置新密码
     *
     * @param phoneNum 电话号码
     * @param what     handler传递值
     */
    public void forgotPwdUpdateNew(String phoneNum, String pwd, String mobileCode, int what) {
        process(new ProcessResult() {
            @Override
            public void onResult(Response res, JSONObject result) throws Exception {
                if (res.isSuccessful) {
                    ALog.e(TAG, "忘记密码  设置新密码------------>" + result);
                }
            }
        }, URLConst.FORGOT_PWD_UPDATE_NEW, what, "userPhone", phoneNum, "password", pwd, "mobileCode", mobileCode);
    }

    /**
     * 获取绑定手机的验证码
     *
     * @param phone 传递的验证码
     * @param what  handler传递值
     */
    public void getBindPhoneVerifyCodeUrl(String phone, int what) {
        process(new ProcessResult() {
            @Override
            public void onResult(Response res, JSONObject result) throws Exception {
                if (res.isSuccessful) {
                    ALog.e(TAG, "获取绑定手机的验证码----------------->" + result);
                }
            }
        }, URLConst.GET_BIND_PHONE_VERIFY_CODE_URL, what, "userPhone", phone);
    }

    /**
     * 绑定手机判断验证码
     *
     * @param registerKey 被打包后的账号+验证码
     * @param what        handler传递值
     */
    public void judgeVerifyCodeByBindPhone(final String registerKey, String code, int what) {
        process(new ProcessResult() {
            @Override
            public void onResult(Response res, JSONObject result) throws Exception {
                if (res.isSuccessful) {
                    ALog.e(TAG, "绑定手机判断验证码------------>" + res);
                }
            }
        }, URLConst.JUDGE_VERIFY_CODE_BY_BIND_PHONE_URL, what, "mobileCode", code, "userPhone", registerKey);
    }


    /**
     * 获取设置支付密码的验证码
     *
     * @param phone 传递的验证码
     * @param what  handler传递值
     */
    public void getSetPayPwdVerifyCodeUrl(String phone, int what) {
        process(new ProcessResult() {
            @Override
            public void onResult(Response res, JSONObject result) throws Exception {
                if (res.isSuccessful) {
                    ALog.e(TAG, "获取设置支付密码的验证码----------------->" + result);
                }
            }
        }, URLConst.GET_SET_PAY_PWD_VERIFY_CODE_URL, what, "userPhone", phone);
    }


    /**
     * 设置支付密码判断验证码
     *
     * @param registerKey 被打包后的账号+验证码
     * @param what        handler传递值
     */
    public void judgeVerifyCodeBySetPayPwd(final String registerKey, String code, int what) {
        process(new ProcessResult() {
            @Override
            public void onResult(Response res, JSONObject result) throws Exception {
                if (res.isSuccessful) {
                    ALog.e(TAG, "设置支付密码判断验证码------------>" + res);
                }
            }
        }, URLConst.JUDGE_VERIFY_CODE_BY_GET_PAY_PWD_URL, what, "mobileCode", code, "userPhone", registerKey);
    }


    /**
     * 修改支付密码 or 设置支付密码
     *
     * @param oldPwd 旧密码
     * @param newPwd 新密码
     * @param what   handler传递值
     */
    public void modifyPayPasswordUrl(String oldPwd, String newPwd, int what) {
        if (newPwd != null && newPwd.equals("")) {//  忘记支付密码  或  设置支付密码
            process(new HandlerRequestImpl.EmptyProcessResult(), URLConst.MODIFY_PAY_PASSWORD_URL, what, "newPass", oldPwd);
        } else {// 修改支付密码
            process(new HandlerRequestImpl.EmptyProcessResult(), URLConst.MODIFY_PAY_PASSWORD_URL, what, "oldPass", oldPwd, "newPass", newPwd);
        }
    }

    /**
     * 忘記支付密碼獲取驗證碼
     *
     * @param phone 电话号码
     * @param what  handler传递值
     */
    public void getForgotPayPwdVerifyCodeUrl(String phone, int what) {
        process(new ProcessResult() {
            @Override
            public void onResult(Response res, JSONObject result) throws Exception {
                if (res.isSuccessful) {
                    ALog.e(TAG, "忘記支付密碼獲取驗證碼----------------->" + result);
                }
            }
        }, URLConst.GET_FORGOT_PWD_PHONE_VERIFY_CODE_URL, what, "userPhone", phone);
    }

    /**
     * 忘记支付密码判断验证码
     *
     * @param registerKey 被打包后的账号+验证码
     * @param what        handler传递值
     */
    public void judgeVerifyCodeByForgotPayPwd(String registerKey, String verifyCode, int what) {
        process(new ProcessResult() {
            @Override
            public void onResult(Response res, JSONObject result) throws Exception {
                if (res.isSuccessful) {
                    ALog.e(TAG, "设置支付密码判断验证码------------>" + res);
                }
            }
        }, URLConst.JUDGE_VERIFY_CODE_BY_FORGOT_PAY_PWD_URL, what, "phoneCode", verifyCode, "userPhone", registerKey);
    }

    /**
     * 忘记支付密码  设置支付密码
     *
     * @param pwd        密码
     * @param mobileCode 验证码
     * @param what       handler传递值
     */
    public void forgotPayPasswordUrl(String pwd, String mobileCode, int what) {
        process(new ProcessResult() {
            @Override
            public void onResult(Response res, JSONObject result) throws Exception {
                if (res.isSuccessful) {
                    ALog.e(TAG, "忘记支付密码  设置支付密码------------>" + res);
                }
            }
        }, URLConst.FORGOT_PWD_SET_PAY_PWD_URL, what, "payPwd", pwd, "phoneCode", mobileCode);
    }

    /**
     * 绑定手机并设置密码
     *
     * @param phoneNum 电话号码
     * @param what     handler传递值
     */
    public void bindPhoneSetPwd(String phoneNum, String mobileCode, String pwds, int what) {
        process(new ProcessResult() {
            @Override
            public void onResult(Response res, JSONObject result) throws Exception {
                if (res.isSuccessful) {
                    ALog.e(TAG, "绑定手机并设置密码------------>" + result);
                }
            }
        }, URLConst.BIND_PHONE_SET_PWD_URL, what, "userPhone", phoneNum, "mobileCode", mobileCode, "password", pwds);
    }

    /**
     * 获取登录的短信验证码
     *
     * @param phone 电话号码
     * @param what  handler传递值
     */
    public void getPhoneLoginSendSms(String phone, int what) {
        process(new ProcessResult() {
            @Override
            public void onResult(Response res, JSONObject result) throws Exception {
                if (res.isSuccessful) {
                    ALog.e(TAG, "获取登录的短信验证码-------------------->" + result);
                    res.obj = result.getJSONObject(DATA_KEY).createObject(VerifyBean.class);
                }
            }
        }, URLConst.GET_PHONE_LOGIN_SEND_SMS_URL, what, "userPhone", phone);
    }


    /**
     * 版本判断
     *
     * @param i          手机类型
     * @param nowVersion 当前用户版本
     * @param what       handler传递值
     */
    public void versionCheck(int i, String nowVersion, int what) {
        process(new ProcessResult() {
            @Override
            public void onResult(Response res, JSONObject result) throws Exception {
                if (res.isSuccessful) {
                    ALog.e(TAG, "show version ----------------------->" + result);
                    res.obj = result.getJSONObject(DATA_KEY).createObject(VersionBean.class);
                }
            }
        }, URLConst.VERSION_OF_THE_JUDGMENT, what, "phoneSrc", i, "version", nowVersion);
    }

    /**
     * 忘记密码的短信
     *
     * @param phone
     * @param what
     */
    public void getVerifyCodeForgot(String phone, int what) {
        process(new ProcessResult() {
            @Override
            public void onResult(Response res, JSONObject result) throws Exception {
                if (res.isSuccessful) {
                    ALog.e(TAG, "获取登录的短信验证码-------------------->" + result);
                    res.obj = result.getJSONObject(DATA_KEY).createObject(VerifyBean.class);
                }
            }
        }, URLConst.GET_FORGOT_LOGIN_SEND_SMS_URL, what, "userPhone", phone);
    }

    public void setPasswordUrl(String pwd1, String messageId, String result,String code, int what) {
        process(new HandlerRequestImpl.EmptyProcessResult(), URLConst.FORGOT_PWD_SET_PWD_URL, what, "newPassword", pwd1, "userPhone", result, "messageId", messageId,"mobileCode", code);
    }

    /**
     * 初始化支付密码
     */
    public void initPasswordUrl(String pwd, String name, String bank, String card, int what) {
        process(new HandlerRequestImpl.EmptyProcessResult(), URLConst.INIT_PAY_PWD_URL, what, "userName", name, "bankName", "-", "cardNumber", bank, "payPwd", pwd);
    }
}